<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SyncSAP
 *
 * @author Administrator
 */
class SyncSAP extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('/wms/inventory_track_service');
        $this->load->service('/wms/sap_service');
        $this->load->service('/wms/warehouse_list_service');
    }

    function sync_no_transaction()
    {
        $where = array(
            'sync_status' => 1,
            'type <> '=>1
        );
        $track_list = $this->inventory_track_service->get_list($limit = 0, $pagesize = 100, $orderstr = '', $where, $wherein = '', $like = '');
//        echo '<br>';
//        print_r($track_list);
//        exit;
        foreach ($track_list as $track_info)
        {
            $type = $track_info['type'];
            $row_id = $track_info['id'];
            $sku = $track_info['sku'];
            $in_or_out = $track_info['in_or_out'];
            $quantity = $track_info['quantity'];
            $ref_num = $track_info['ref_num'];
            $whs_code = $track_info['whs_code'];
            $comments = $track_info['comments'];
            //获取科目代码
            $acct_code = $this->warehouse_list_service->get_acctcode($whs_code, $type);
            $whs_code_s = $this->warehouse_list_service->get_whs_code_s($whs_code);
            if (!$acct_code or !$whs_code_s)
            {   echo $acct_code."#".$whs_code_s."<br>";
                echo $row_id.' acct_cod or whs_code_s is null<br/>';
                continue;
            }
            $IGE1Arr = array();
            $OIGEArr = array();
            $OIGEArr = array(array(
                    'DocDate' => date('Y/m/d H:i:s', time()),
                    'TaxDate' => date('Y/m/d H:i:s', time()),
                    'U_jockey' => '1',
                    'U_OwnerCode' => '1',
                    'Comments' => 'WMS-' . $type . '-' . $row_id . '-' . $ref_num . '-' . $comments,
                    'U_Type' => $type
                    ));
            $IGE1Arr = array(array(
                    'LineNum' => 0,
                    'ItemCode' => $sku,
                    'Quantity' => $quantity,
//                    'Price' => 0,
                    'WhsCode' => $whs_code_s,
                    'AcctCode' => $acct_code
                    ));

            if ('in' == $in_or_out)
            { 
//            $IGE1Arr[0]['PriceBefDi']="50";
                //获取SAP成本
                $sap_sku_info = $this->sap_service->get_sku_inventory($sku, $whs_code_s);
                var_dump($sap_sku_info);
                if (!is_numeric($sap_sku_info[0]['成本']))
                {
                    echo $row_id . 'sync failed,sku cost <=0<br/>';
                    continue;
                }
                $IGE1Arr[0]['PriceBefDi'] = $sap_sku_info[0]['成本'];
                $insert = $this->sap_service->no_transaction_receipt($OIGEArr, $IGE1Arr);
            }
            elseif ('out' == $in_or_out)
            {
                $insert = $this->sap_service->no_transaction_delivery($OIGEArr, $IGE1Arr);
            }
            var_dump($insert);
            echo '<br>';
            if ($insert['Result'] === true)
            {
                $data = array(
                    'sync_status' => 2,
                    'sap_ref' => $insert['DocEntry'],
                );
                $where_up = array(
                    'id' => $row_id
                );
                $up = $this->inventory_track_service->update_track($data, $where_up);
                var_dump($up);
                echo '<br>';
            }
            else
            {
                echo $row_id . 'sync failed<br/>';
            }
        }
    }

}

?>
